package com.banshuibao.bsb_android.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.adapter.ExpertAnswerAdapter;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.ExpertListModel;
import com.banshuibao.bsb_android.model.ExpertResultModel;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshBase;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshListView;
import com.banshuibao.bsb_android.util.IntentManager;

import java.util.List;

public class MenuTagResultActivity extends BaseActivity implements PullToRefreshListView.OnRefreshListener2, AdapterView.OnItemClickListener {

    private PullToRefreshListView pullToRefreshListView;
    private ListView listView;
    private ExpertAnswerAdapter expertAnswerAdapter;
    private int startId,endId;
    private boolean isUp = false;
    private String uuid,type;
    private int rows;
    private LinearLayout.LayoutParams params;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_menu_tag_result);
        String name = getIntent().getStringExtra("name");
        type = getIntent().getStringExtra("type");
        uuid = getIntent().getStringExtra("uuid");
        setTitleText(name.trim());
        initView();
        getData();
    }

    public void initView(){
        pullToRefreshListView = findViewById(R.id.ptrlv);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        pullToRefreshListView.loadBar();
        pullToRefreshListView.setOnRefreshListener(this);
        listView = pullToRefreshListView.getRefreshableView();
        listView.setDivider(null);
        Drawable drawable = getResources().getDrawable(R.drawable.noloading);
        ImageView imageView = new ImageView(this);
        params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER;
        imageView.setLayoutParams(params);
        imageView.setImageDrawable(drawable);
        listView.setEmptyView(imageView);
        expertAnswerAdapter = new ExpertAnswerAdapter(this,true);
        listView.setAdapter(expertAnswerAdapter);
        listView.setOnItemClickListener(this);
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        getData();

    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        isUp = true;
        getNetRequest();
    }

    public void getData(){
        isUp = false;
        startId = 1;
        if(expertAnswerAdapter!=null){
            int count = expertAnswerAdapter.getCount();
            if(count>=10){
                endId += count-10;
            }
            endId += startId+5;
        }
        getNetRequest();
    }

    public void getNetRequest(){
        ExpertListModel listModel = new ExpertListModel();
        listModel.setSinceId(startId);
        listModel.setMaxId(endId);
        listModel.setType(type);
        listModel.setUuid(uuid);
        HttpUtil.doPost(Constants.Url.EXPERT_ANSWER, listModel, new HttpResponse(this, ExpertResultModel.class) {
            @Override
            public void onResult(Object obj) {
                ExpertResultModel model = (ExpertResultModel) obj;
                rows = model.getRows();
                startId += rows;
                List<ExpertResultModel.DataBean> data = model.getData();
                if (isUp) {
                    expertAnswerAdapter.setUpData(data);
                } else {
                    expertAnswerAdapter.setDownDate(data);
                }
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                pullToRefreshListView.onRefreshComplete();
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        if (expertAnswerAdapter != null && expertAnswerAdapter.getCount() > 0) {
            ExpertResultModel.DataBean dataBean = expertAnswerAdapter.getItem(i - 1);
            IntentManager.goExpertDetailActivity(MenuTagResultActivity.this, dataBean.getUuid());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(expertAnswerAdapter!=null){
            expertAnswerAdapter.clear();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(expertAnswerAdapter!=null){
            expertAnswerAdapter.stopMedia();
        }
    }
}
